﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.Xpo;
namespace ChuanPlus.DAO
{
    public class StockDAO
    {
        
        /// <summary>
        /// substract product quantity from warehouse
        /// </summary>
        /// <param name="warehouse"></param>
        /// <param name="product"></param>
        /// <param name="QtyToSubtract"></param>
        /// <returns></returns>
        public static bool subtract(Warehouse warehouse, Product product, double QtyToSubtract) // chua check
        {
            // lay stockObj co trong db or tao moi
            XPQuery<Stock> stock = new XPQuery<Stock>(Session.DefaultSession);
            var listStock = from s in stock
                            where (s.ID == product && s.warehouseID == warehouse)
                            select s;
            List<Stock> arrStock = new List<Stock>(listStock.AsEnumerable<Stock>());
            Stock stockObj;
            if (arrStock.Count == 0)
            {
                stockObj = new Stock(Session.DefaultSession, product, warehouse, 0);
                stockObj.QtyBalance -= QtyToSubtract;
            }
            else
            {
                stockObj = arrStock[0];
                stockObj.QtyBalance -= QtyToSubtract;
            }
            stockObj.Save();
            return true;
        }
        
        /// <summary>
        /// move product between warehouse
        /// </summary>
        /// <param name="warehouseFrom"></param>
        /// <param name="warehouseTo"></param>
        /// <param name="product"></param>
        /// <param name="QtyToMove"></param>
        /// <returns></returns>
        public static bool move(Warehouse warehouseFrom, Warehouse warehouseTo, Product product, double QtyToMove) //chua check
        {
            // tru qty o stock cu~
            subtract(warehouseFrom, product, QtyToMove);
            // cong (or add new) cho qty o stock moi
            add(warehouseTo, product, QtyToMove);
            return true;
        }
       
        /// <summary>
        ///add product quantity from warehouse
        /// </summary>
        /// <param name="warehouse"></param>
        /// <param name="product"></param>
        /// <param name="QtyToAdd"></param>
        /// <returns></returns>
        public static bool add(Warehouse warehouse, Product product, double QtyToAdd) // chua check
        {
            XPQuery<Stock> stock = new XPQuery<Stock>(Session.DefaultSession);
            var listStock = from s in stock
                            where (s.ID == product && s.warehouseID == warehouse)
                            select s;
            List<Stock> arrStock = new List<Stock>(listStock.AsEnumerable<Stock>());
            Stock stockObj;
            if (arrStock.Count == 0)
            {
                stockObj = new Stock(Session.DefaultSession, product, warehouse, 0);
                stockObj.QtyBalance += QtyToAdd;
            }
            else
            {
                stockObj = arrStock[0];
                stockObj.QtyBalance += QtyToAdd;
            }
            stockObj.Save();
            return true;
        }
        /// <summary>
        /// Get Qtybalance base on warehouse name and product
        /// </summary>
        /// <param name="warehouseName"></param>
        /// <param name="product"></param>
        /// <returns></returns>
        public static double GetProductBalance(string warehouseName, Product product)
        {
            Session session = new Session();
            XPQuery<Stock> stocks = new XPQuery<Stock>(session);
            var lst = (from s in stocks
                       where s.warehouseID.warehouseName == warehouseName
                       && s.ID.ID == product.ID
                       select s).Take(1);
            Stock item = lst.First();
            return item.QtyBalance;
        }
	/// <summary>
        /// Get stock by product and warehousename
        /// </summary>
        /// <param name="product"></param>
        /// <param name="warehouseName"></param>
        /// <returns></returns>
        public static Stock GetStock(int productID, int warehouseid)
        { 
            
            XPQuery<Stock> stocks = new XPQuery<Stock>(Session.DefaultSession);
            var list = (from st in stocks
                       where st.warehouseID.warehouseId == warehouseid
                       && st.ID.ID == productID
                       select st).Take(1);
            return list.First();
        }
        /// <summary>
        /// Get all product of specify warehouse
        /// </summary>
        /// <param name="warehouse"></param>
        /// <returns></returns>
        public static List<Product> ListProductByWarehouseName(string warehouseName)
        {
            XPQuery<Stock> stocks = new XPQuery<Stock>(Session.DefaultSession);
            var list = (from st in stocks
                        where st.warehouseID.warehouseName == warehouseName
                        select st);
            List<Product> result = new List<Product>();
            foreach (Stock item in list)
            {
                result.Add(item.ID);
            }
            
            return result;
        }

        
    }

}
